using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;
using RS.DOM.Types;

namespace RS.DOM.Construction
{
    public enum StrategyType
    {
        Repeat
    }

    public class Group: Line
    {
        #region Constructors

        public Group(Context context)
            :base(context)
        {
            base.Context = new Context(this.Context.Document, this, null); //override
            m_name = string.Empty;
            m_expressions = new ExpressionCollection();
            m_strategy = StrategyType.Repeat;
        }

        #endregion

        #region Members

        private string m_name;
        private ExpressionCollection m_expressions;
        private StrategyType m_strategy;
        private Collection m_numbers;

        #endregion

        #region Properties

        public StrategyType Strategy
        {
            get
            {
                return m_strategy;
            }
        }
        
        public ExpressionCollection Expressions
        {
            get
            {
                return m_expressions;
            }
        }

        public Collection Numbers
        {
            get
            {
                return m_numbers;
            }
        }

        #endregion

        #region Methods

        private void ModifyItems()
        {
            if (m_strategy == StrategyType.Repeat)
            {
                //nothing;
            }
        }

        public override void Build()
        {
            if (this[0].TokenType == TokenType.Name)
            {
                m_name = this[0].Text;
            }

            Expression expression = null;
            foreach (Token token in this)
            {
                if (token.TokenType == TokenType.Bracket &&
                    token.Text == "[")
                {
                    expression = new Expression(this.Context);
                }
                else if (token.TokenType == TokenType.Bracket &&
                         token.Text == "]")
                {
                    this.Expressions.Add(expression);
                    expression = null;
                }
                else
                {
                    if (expression != null)
                    {
                        expression.Add(token);
                    }
                }
            }

            this.Expressions.Build();
        }

        public void CopyArea()
        {
            m_numbers = new Collection(this.Context);

            for (int i = 0; i < this.Context.Document.Area.Width; i++)
                for (int j = 0; j < this.Context.Document.Area.Height; j++)
                {
                    m_numbers.Result.Add(new Number(this.Context, (int)this.Context.Document.Area.GetItem(i, j).Value));
                }
        }

        public override Token Execute()
        {
            this.CopyArea();

            while (true)
            {
                this.ModifyItems();
                bool result = true;

                foreach (Expression expression in this.Expressions)
                {
                    Bool token = (Bool)expression.BuildAndExecute();
                    result = result && token.Result;

                    if (!result)
                        break;
                }

                if (result)
                    return m_numbers;
            }

            m_numbers.Result.Clear();

            return m_numbers;
        }

        #endregion
    }
}
